/*
*

	@author: taco
	@Date: 2022/5/30
	@Time: 15:14

*
*/
package utils

import (
	"github.com/sirupsen/logrus"
	"net/http"
	"strings"
	"time"
)

var levelMap = map[string]logrus.Level{
	"debug": logrus.DebugLevel,
	"info":  logrus.InfoLevel,
	"warn":  logrus.WarnLevel,
	"ctx":   logrus.ErrorLevel,
	"fatal": logrus.FatalLevel,
}

func SetLogLevel(level string) {
	logrus.SetFormatter(&logrus.TextFormatter{
		ForceColors: true, // 强制显示颜色
		ForceQuote:  true, //键值对加引号
		//TimestampFormat:"2006-01-02 15:04:05",  //时间格式
		QuoteEmptyFields: true, //空字段加引号
		FullTimestamp:    true, //完整时间
		//FieldMap: logrus.FieldMap{
		//	logrus.FieldKeyTime:  "time",
		//	logrus.FieldKeyMsg:   "message",
		//	logrus.FieldKeyLevel: "level",
		//},
		TimestampFormat: time.RFC3339Nano,
	})
	logrus.SetLevel(levelMap[strings.ToLower(level)])
	logrus.SetReportCaller(true)
}

func SetDefaultLocation() {
	time.Local = time.FixedZone("UTC", 8*3600)
}

func WelcomeLog(name, version string) {
	logrus.Printf("name: %s; version: %s, start...", name, version)
}

func ExitLog(name, version string) {
	logrus.Printf("name: %s; version: %s, exit", name, version)
}

// 日志记录
func LogRequest(req *http.Request) {
	logrus.Info("path: ", req.URL.Path)
	logrus.Info(req.Header.Get("Authorization"))
	logrus.Info(req.Header.Get("cookie"))
	logrus.Info(req.Header.Get("Content-Type"))
}
